home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 672 < prev    next >
Encoding:
Text File  |  1996-08-05  |  8.2 KB  |  263 lines

  1. Path: gate.net!pslfl2-10
  2. From: bhutto@gate.net (William Hutto)
  3. Newsgroups: alt.msdos.programmer,comp.lang.c
  4. Subject: Re: Two C problems
  5. Date: 8 Jan 1996 11:45:37 GMT
  6. Organization: CyberGate, Inc.
  7. Message-ID: <4cr051$1quo@news.gate.net>
  8. References: <4cojnn$rgd@lugb.latrobe.edu.au>
  9. NNTP-Posting-Host: pslfl2-10.gate.net
  10. X-Newsreader: News Xpress Version 1.0 Beta #4
  11.  
  12. In article <4cojnn$rgd@lugb.latrobe.edu.au>,
  13.    csigjb@luxor.latrobe.edu.au () wrote:
  14. >I have two C problems.
  15. >
  16. >PROBLEM 1 :
  17. >
  18. >int main(int argc,char *argv[])
  19. >{
  20. >     FILE *file;
  21. >     char x,ch,width,height,back,fore,stringpos;
  22. >     int numlines,linenum;
  23. >     text_info info;
  24. >     nodetypeptr listptr,posptr;
  25. >
  26. >
  27. >     if (argc<2)
  28. >     {
  29. >          cputs("\nUsage : show file name [fore ground color] [back ground 
  30. color] . . .\n");
  31. >     }
  32. >     else
  33. >     {
  34. >          file=fopen(argv[1],"rt");
  35. >          if (file==NULL)
  36. >          {
  37. >               cprintf("\nFile %s not found . . .\n",argv[1]);
  38. >          }
  39. >          else
  40. >          {
  41. >               rewind(file);
  42. >               initialize(&listptr);
  43. >               linenum=1;
  44. >               while (fgets(line,maxstring,file)!=NULL)
  45. >               {
  46. >                    removelineends(line,maxstring);
  47. >                    add(&listptr,line,linenum);
  48. >                    linenum++;
  49. >               }
  50. >               numlines=linenum--;
  51. >               fclose(file);
  52. >               posptr=listptr;
  53. >               gettextinfo(&info);
  54. >               if (argc>2)
  55. >               {
  56. >                    getcolors(&fore,&back,argv[2],argv[3]);
  57. >               }
  58. >               else
  59. >               {
  60. >                    fore=LIGHTGRAY;
  61. >                    back=BLACK;
  62. >               }
  63. >               height=info.screenheight;
  64. >               width=info.screenwidth;
  65. >               strcpy(bottomline,"  \30  \31  \33  \32  pgup  pgdn  s 
  66. (search)  help (h) esc (quit)");
  67. >               for(x=strlen(bottomline)+1;x<width;x++)
  68. >               {
  69. >                    strcat(bottomline," ");
  70. >               }
  71. >               _setcursortype(_NOCURSOR);
  72. >               clrscr();
  73. >               textcolor(back);
  74. >               textbackground(fore);
  75. >               gotoxy(1,height);
  76. >               cputs(bottomline);
  77. >               textcolor(fore);
  78. >               textbackground(back);
  79. >               height--;
  80. >               linenum=1;
  81. >               stringpos=0;
  82. >               
  83. writescreenfull(width,height,posptr,stringpos,searchstring,fore,back);
  84. >               ch=space;
  85. >               while (ch!=escape)
  86. >               {
  87. >                    while ((ch!=up) && (ch!=down) && (ch!=pageup) &&
  88. >                           (ch!=pagedown) && (ch!=escape) && (ch!=left) &&
  89. >                           (ch!=right) && (ch!=end_) && (ch!=home) &&
  90. >                           (ch!=search) && (ch!=help))
  91. >                    {
  92. >                         ch=getch();
  93. >                         if (ch==nul)
  94. >                         {
  95. >                              ch=getch();
  96. >                         }
  97. >                    }
  98. >                    if (ch==up)
  99. >                    {
  100. >                         if ((linenum-1)>=1)
  101. >                         {
  102. >                              linenum--;
  103. >                         }
  104. >                         else
  105. >                         {
  106. >                              linenum=1;
  107. >                         }
  108. >                         setpos(&posptr,linenum);
  109. >
  110. >                    }
  111. >                    else if (ch==down)
  112. >                    {
  113. >                         if ((linenum+1+height)<=numlines)
  114. >                         {
  115. >                              linenum++;
  116. >                         }
  117. >                         else
  118. >                         {
  119. >                              linenum=numlines-height+1;
  120. >                         }
  121. >                         setpos(&posptr,linenum);
  122. >                    }
  123. >                    else if (ch==pageup)
  124. >                    {
  125. >                         if ((linenum-height+1)>=1)
  126. >                         {
  127. >                              linenum=linenum-height+1;
  128. >                         }
  129. >                         else
  130. >                         {
  131. >                              linenum=1;
  132. >                         }
  133. >                         setpos(&posptr,linenum);
  134. >                    }
  135. >                    else if (ch==pagedown)
  136. >                    {
  137. >                         if (((linenum+height-1)<=numlines) && 
  138. ((numlines-(linenum+height-1))>=(height-1)))
  139. >                         {
  140. >                              linenum=linenum+height-1;
  141. >                         }
  142. >                         else
  143. >                         {
  144. >                              linenum=numlines-height+1;
  145. >                         }
  146. >                         setpos(&posptr,linenum);
  147. >                    }
  148. >                    else if (ch==left)
  149. >                    {
  150. >                         if (stringpos>0)
  151. >                         {
  152. >                              stringpos--;
  153. >                         }
  154. >                    }
  155. >                    else if (ch==right)
  156. >                    {
  157. >                         if (stringpos<=(maxstring-width))
  158. >                         {
  159. >                              stringpos++;
  160. >                         }
  161. >                    }
  162. >                    else if (ch==end_)
  163. >                    {
  164. >                         stringpos=maxstring-width+1;
  165. >                    }
  166. >                    else if (ch==home)
  167. >                    {
  168. >                         stringpos=0;
  169. >                    }
  170. >                    else if (ch==help)
  171. >                    {
  172. >                         displayhelp(fore,width,height);
  173. >                    }
  174. >                    else if (ch==search)
  175. >                    {
  176. >                         
  177. readstring(searchstring,bottomline,height,fore,back);
  178. >                    }
  179. >                    if (ch!=escape)
  180. >                    {
  181. >                         ch=space;
  182. >                    }
  183. >                    
  184. writescreenfull(width,height,posptr,stringpos,searchstring,fore,back);
  185. >               }
  186. >               while (!empty(listptr))
  187. >               {
  188. >                    remove(&listptr);
  189. >               }
  190. >               height++;
  191. >               window(1,1,width,height);
  192. >               clrscr();
  193. >               _setcursortype(_NORMALCURSOR);
  194. >          }
  195. >     }
  196. >     return(0);
  197. >}
  198. >
  199. >This program should exit such that the screen is clear with the DOS prompt
  200. >in the upper left corner, but no, it exits with a short line of text on the
  201. >first line and then the DOS prompt on the next line. I never (or rarely)
  202. >have these sort of problems with Pascal so why do they continuously crop up
  203. >with C.
  204. >
  205. >PROBLEM 2 :
  206. >
  207. >const escape=27;
  208. >const cr=13;
  209. >const bs=8;
  210. >
  211. >while (ch!=cr)
  212. >{
  213. >     .
  214. >     .
  215. >     .
  216. >}
  217. >
  218. >while (ch!=escape)
  219. >{
  220. >     .
  221. >     .
  222. >     .
  223. >}
  224. >
  225. >If I replace the 27 with \27' or the 13 with '\13' then these loops don't
  226. >work i.e. an infinite loop results. WHY?
  227. >
  228. >Also if I want to do somthing like this : puts("\24 \25"); which should
  229. >print the up and down arrows on the screen (ASCII 24 and 25), but no, it
  230. >prints some other ASCII characters. As far as I can tell C has its own
  231. >unique character table, at least for the control characters. WHY?
  232. >
  233. >Sometimes I wonder whether some one needs to go back an rewrite the damn
  234. >language so that it works sensibly and predictably like Pascal can be
  235. >relied upon to do.
  236.  
  237. There isn't a problem with the C language. You just need to build up to where 
  238. you want to be (apparantly as competant in C as you consider yourself to be in 
  239. Pascal). I just scrolled through your code without even stopping and noticed 
  240. you passed up the switch() statement for else if() for example. Be more 
  241. thorough in your studies. An example:
  242.  
  243.     switch(ch) {
  244.         case up:
  245.             /*do the up thing*/
  246.             break;
  247.         case down:
  248.             /*get down!*/
  249.             break;
  250.         case sideways:
  251.             ...
  252.             /*and so forth*/
  253.  
  254. I figure you're competant enough. Just slow down and learn the syntax and 
  255. constructs. There's not many. Some of them are gonna be different from what 
  256. you're used to. The biggest part in terms of memorization is learning the 
  257. standard library functions. It's a good language, you just have to get over 
  258. your bias for Pascal.
  259.  
  260. Bill
  261.  
  262. "Whatcha got on?...Your mind?"
  263.